﻿var typeDetailFormVue = new Vue({
    el: "#typeDetailForm",
    data: {
        isNewType: true,
        type: {
            ID: 0,
            Name: ""
        }
    },
    methods: {
        SaveType: function (event) {
            event.preventDefault();
            SaveEquipmentType(this);
        },
        UpdateType: function (event) {
            event.preventDefault();
            UpdateEquipmentType(this);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        var isNew = GetQueryString("IsNew");
        var typeID = GetQueryString("TypeID");
        if (isNew == "true") {
            this.isNewType = true;
        } else {
            this.isNewType = false;
            GetEquipmentType(this, typeID);
        }
    },
    watch: {
        type: {
            handler: function (val, oldVal) {
                if (val.Name.length > 0)
                    CheckEquipmentType(this);
            },
            deep: true
        }
    }
});

/**
 * 查询设备类型
 */
function GetEquipmentType(vue, typeID) {
    parent.ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Equipment/GetEquipmentTypeByID?TypeID=" + typeID + "&v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.type = res.data;
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            toastr.error("加载设备类型信息失败，请稍候再试Failed to load device type information. Please try again later。");
        }
    });
}

/**
 * 保存设备类型
 */
function SaveEquipmentType(vue) {
    if (CheckEquipmentType(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Equipment/SaveEquipmentType",
            data: { type: vue.type },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存设备类型成功Device type saved successfully。");
                    ResetForm(vue);
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("设备类型信息保存失败，请稍候再试Failed to save the device type information. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    } 
}

/**
 * 更新设备类型
 */
function UpdateEquipmentType(vue) {
    if (CheckEquipmentType(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Equipment/UpdateEquipmentType",
            data: { type: vue.type },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新设备类型成功Device type updated successfully。");
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("设备类型信息更新失败，请稍候再试Failed to update device type information. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    } 
}

/**
 * 检查设备类型数据
 */
function CheckEquipmentType(vue) {
    $("#typeName").removeClass("has-error"); 
    if (vue.type.Name == null || vue.type.Name.length == 0) {
        $("#typeName").addClass("has-error");
        toastr.error("请填写设备类型名称Please fill in the equipment type name。")
        return false;
    }
    return true;
}

/**
 * 清空信息
 */
function ResetForm(vue) {
    vue.type = {
        ID: 0,
        Name: ""
    }
}